creating a Request

Creating a Request

Below is an example implementation of a Request

import communicator from './communicator';

communicator.Request({

  // optional, if ommitted request will be global
  context: 'user',
  // name of the request, if context specified, specific to its context
  name: 'findById',
  // the connection to execute this request with, otherwise uses the default specified in the communicator
  connection: 'local-xhr',
  // the route of the requst, allowed to contain :splats
  route: '/user/:id',
  // optional, called when the request was successful, should return a transformed response value or a Promise that resolves with the desired response value
  resolve(responseBody, requestBody) {
    return Promise.resolve(responseBody);
  },
  // the same as resolve but for failed requests,
  // this allows you to resolve a Request even if its actual request failed
  reject(responseBody, requestBody) {
    return Promise.reject(responseBody);
  }
});

Executing the Request

The Request created above is now available, and we can call it as described below.

this is the preferred way of executing a Request.

communicator.servers['local-xhr'].user.findById(56)
  .then(...);
// or
communicator.connections['local-xhr'].server.user.findById(56)
  .then(...);

The Request itself is exposed on the requests object.

communicator.requests['local-xhr'].user.findById.execute(56)
  .then(...);
// or
communicator.connection['local-xhr'].requests.user.findById.execute(56)
    .then(...);

if context were omitted the Request would've been available under:

communicator.requests['local-xhr'].findById(56)
  .then(...);
// or
communicator.connections['local-xhr'].requests.findById.execute(56)
  .then(...);
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.4.0 on 2015-12-01T13:53:50+01:00